
// skin variables
MuseX.SkinList = {};

// hook lists
MuseX.HookList = {};


/*------------------------------------
	reload skins command
------------------------------------*/
concommand.Add( "musex_reloadskins", function( pl, cmd, args )

	// reload
	MuseX.ReloadSkins();

end );


/*------------------------------------
	load skins from directory
------------------------------------*/
function MuseX.LoadSkins( )

	// skin path
	local path = "musex/skins/";

	// find .lua files in path
	local skins = file.FindInLua( path .. "*.lua" );
	
	// loop through list of files
	for _, skin in pairs( skins ) do
	
		// create skin object
		_G[ 'SKIN' ] = {};
	
		// include file
		include( path .. skin );
		
		// nil object
		_G[ 'SKIN' ] = nil;
		
	end

end


/*------------------------------------
	reload skins
------------------------------------*/
function MuseX.ReloadSkins( )

	// load the skins
	MuseX.LoadSkins();
	
	// merge the skin
	MuseX.MergeSkin();
	
	// apply the skin
	MuseX.ApplySkin();
	
end


/*------------------------------------
	add new skin hook
------------------------------------*/
function MuseX.SkinHook( name, panel, func )

	// default table
	MuseX.HookList[ name ] = MuseX.HookList[ name ] or {};
	
	// store hook
	MuseX.HookList[ name ][ #MuseX.HookList[ name ] + 1 ] = { Panel = panel, Func = func };

end


/*------------------------------------
	call skin hook
------------------------------------*/
function MuseX.CallSkinHook( name, ... )
	
	// make sure the hook exists
	if ( !MuseX.HookList[ name ] ) then
	
		// doesn't exist
		return;
	
	end

	for _, skinhook in pairs( MuseX.HookList[ name ] ) do

		// get the panel associated with the hook
		local panel = skinhook.Panel;
		
		// validate the panel
		if ( panel && panel:IsValid() ) then

			// call the function and pass args
			panel[ skinhook.Func ]( panel, unpack( arg ) );
			
		end
		
	end

end


/*------------------------------------
	define skin by name
------------------------------------*/
function MuseX.DefineSkin( name, tbl )

	// store name
	tbl.Name = name;
	
	// add to list
	MuseX.SkinList[ name ] = tbl;

end


/*------------------------------------
	merge our skin into default
------------------------------------*/
function MuseX.MergeSkin( )

	// get the skin
	local skin = MuseX.GetSetting( "skin" );

	// get skin table
	local tbl = MuseX.SkinList[ skin ];
	
	// make sure it exists
	if ( tbl ) then
	
		// get default skin
		local defaultskin = derma.GetDefaultSkin();
		
		// merge the tables
		table.Merge( defaultskin, tbl );
	
	end

end


/*------------------------------------
	apply skin
------------------------------------*/
function MuseX.ApplySkin( )

	// validate the vgui
	if ( !MuseX.VGUIValid() ) then
	
		// not valid
		return;
	
	end
	
	// get the skin
	local skin = MuseX.GetSetting( "skin" );
	
	// get skin table
	local tbl = MuseX.SkinList[ skin ];
	
	// make sure it exists, and has an Apply function
	if ( tbl && tbl.Apply ) then
	
		// call apply function
		tbl:Apply( MuseX.VGUI );
	
	end
	
	// store what skin is applied
	MuseX.CurrentlyAppliedSkin = skin;
	
end
